package leetcode.leetcode111;

/**
 * @author czt
 * @version 1.0
 * @since 2024/12/27
 *
 */
public class Leetcode111 {

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if (leftDepth == 0) { // 当左子树为null
            return rightDepth + 1; // 返回右子树深度+1
        }
        if (rightDepth == 0) { // 当右子树为null
            return leftDepth + 1; // 返回左子树深度+1
        }
        return Math.min(leftDepth, rightDepth) + 1;
    }
}
